import net.chrisrichardson.ec2deployer.cluster.*
import net.chrisrichardson.ec2deployer.core.*

class CloudToolsPluginUtil {
  def static config

  static {
        config = new ConfigSlurper().parse(new File("grails-app/conf/CloudTools.groovy").toURL())
  }

  static ClusterManager makeClusterManager() {
    ClusterManager manager = new ClusterManager(makeAWSProperties());
    return manager;
  }

  static AWSProperties makeAWSProperties() {
    File awsPropertiesFile = new File(config.cloudTools.awsPropertiesFile)
    AWSProperties awsProperties = new AWSProperties(
        awsPropertiesFile.getAbsolutePath());
    return awsProperties;
  }

  static ClusterSpec makeClusterSpec(grailsAppName)  {
    ClusterSpec clusterSpec = config.cloudTools.clusterSpec
    clusterSpec.webApp(new File("explodedWar/${grailsAppName}").getAbsolutePath(), grailsAppName);
    return clusterSpec;
  }

  static withCluster(closure) {
      ClusterManager manager = makeClusterManager()
      def cluster = manager.getCluster(config.cloudTools.clusterName)
      closure.call(manager, config.cloudTools.clusterName, cluster)
  }

  static withClusterManager(closure) {
      ClusterManager manager = makeClusterManager()
      closure.call(manager, config.cloudTools.clusterName)
  }

}
